package ui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.Border;
import javax.swing.plaf.BorderUIResource;

import server.MessageHandler;

public class MessageFrame extends JFrame{

	private JPanel msgPanel;
	private JPanel inputPanel;
	private JPanel buttonPanel;
	private JButton exitButton;
	private JButton sendButton;
	private JTextArea inputArea;
	private JTextArea msgArea;
	private String message;
	private JTextArea serverMessageArea;

	public MessageFrame(String title, final MessageHandler messageSender){
		this.setTitle(title);
		this.setSize(800, 600);
		//this.setLocation(point);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		this.setLayout(new GridLayout(1, 1, (int)(this.getWidth()*0.01), (int)(this.getHeight()*0.01)));
		msgPanel = new JPanel();
		msgPanel.setBackground(Color.gray);
		msgPanel.setLocation((int)(this.getWidth()*0.01), (int)(this.getHeight()*0.01));
		msgPanel.setLayout(new BoxLayout(msgPanel, BoxLayout.Y_AXIS));
		
		inputPanel = new JPanel();
		inputPanel.setSize((int)(this.getWidth()*0.48), (int)(this.getHeight()*0.93));
		inputPanel.setBackground(Color.white);
		inputPanel.setLocation((int)(this.getWidth()*0.01), (int)(this.getHeight()*0.01));
		inputPanel.setLayout(new BorderLayout());
		buttonPanel = new JPanel();
		exitButton = new JButton("退出");
		
		sendButton = new JButton("发送");
		sendButton.setEnabled(false);
		sendButton.addActionListener(new ActionListener() {
			
			@Override 
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				message = inputArea.getText();
				synchronized (messageSender) {
					messageSender.notify();
				}
			}
		});

		inputArea = new JTextArea();
		inputArea.setLineWrap(true);
		JScrollPane inputScrollPane = new JScrollPane(inputArea);
		inputScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		
		msgArea = new JTextArea();
		msgArea.setLineWrap(true);
		msgArea.setEditable(false);
		JScrollPane msgScrollPane = new JScrollPane(msgArea);
		msgScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		msgPanel.add(msgScrollPane);
		inputPanel.add(inputScrollPane);
		serverMessageArea = new JTextArea("来自服务器的消息......\n");
		serverMessageArea.setEnabled(false);
		serverMessageArea.setRows(15);
		JScrollPane servMsgScrollPane = new JScrollPane(serverMessageArea);
		servMsgScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		inputPanel.add("North", servMsgScrollPane);
		buttonPanel.add(exitButton);
		buttonPanel.add(sendButton);
		inputPanel.add("South",buttonPanel);
		this.add(msgPanel);
		this.add(inputPanel);
		this.setVisible(true);
		
	}
	
	public static void main(String[] args){
		/*GamePanel frame = new GamePanel("title");
		//frame.addMessage("Me", "11:50", "Hello");
		for(int i = 0; i<10; i++){
			frame.receiveMessage("Me", "11:50", String.valueOf(i));
		}*/
	}
	
	public void receiveMessage(String sender, String time, String content){
		msgArea.append(sender+"  "+time+"\n");
		msgArea.append(content+"\n");
	}
	
	public String getMessage(){
		return this.message;
	}
	
	public void setSenderButtonState(boolean enabled){
		this.sendButton.setEnabled(enabled);
	}

	public void receiveServMsg(String msg){
        serverMessageArea.append(msg+"\n");
    }
}
